home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Packmags
/
Source, The - Issue 5 (1993)(Epsilon)[WB].zip
/
Source, The - Issue 5 (1993)(Epsilon)[WB].adf
/
Source
/
Vectors
/
Rendering
/
Triangle.lha
/
triangle.c
< prev
next >
Wrap
Text File
|
1991-09-04
|
7KB
|
395 lines
void drawTriangle(unsigned x1,unsigned y1,unsigned x2,unsigned y2,unsigned x3,unsigned y3,unsigned char c)
{unsigned int bendCheck,slope1,slope2,slope3,z1,first,z2,last,z3,aux,z4;
unsigned char sign1,sign2,sign3,firstSign,lastSign,auxSign; /* were boolean */
unsigned char bend;
z1=0; z2=0; z3=0; z4=0; /*note: don't take these out. They're nescessary*/
/*do a quick little insertion sort on the points*/
if (y2<y1)
{asm mov ax,y1
asm mov bx,y2
asm mov y1,bx
asm mov y2,ax
asm mov ax,x1
asm mov bx,x2
asm mov x1,bx
asm mov x2,ax /*swap point 1 and point 2*/
}
if (y3<y2)
{asm mov ax,y3
asm mov bx,y2
asm mov y3,bx
asm mov y2,ax
asm mov ax,x3
asm mov bx,x2
asm mov x3,bx
asm mov x2,ax /*swap point 3 and point 2*/
}
if (y2<y1)
{asm mov ax,y1
asm mov bx,y2
asm mov y1,bx
asm mov y2,ax
asm mov ax,x1
asm mov bx,x2
asm mov x1,bx
asm mov x2,ax /*swap point 1 and point 2*/
}
/*make sure that no two points have the same y value*/
if (y1==y2)
{y2++;
y3++;
}
if (y2==y3) y3++;
/*calculate them slopes, avoiding pascal's meddling with assembly*/
if (x1<x2)
{asm mov dx,0
asm mov ax,x2
asm sub ax,x1
asm mov ah,al
asm mov al,0
asm mov bx,y2
asm sub bx,y1
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope2,ax
asm mov byte ptr sign2,0 /*line2 has positive slope*/
}
else
{asm mov dx,0
asm mov ax,x1
asm sub ax,x2
asm mov ah,al
asm mov al,0
asm mov bx,y2
asm sub bx,y1
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope2,ax
asm mov byte ptr sign2,1 /*line2 has negative slope*/
}
if (x3<x1)
{asm mov dx,0
asm mov ax,x1
asm sub ax,x3
asm mov ah,al
asm mov al,0
asm mov bx,y3
asm sub bx,y1
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope1,ax
asm mov byte ptr sign1,1 /*line1 has negative slope*/
}
else
{asm mov dx,0
asm mov ax,x3
asm sub ax,x1
asm mov ah,al
asm mov al,0
asm mov bx,y3
asm sub bx,y1
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope1,ax
asm mov byte ptr sign1,0 /*line1 has positive slope*/
}
if (x3<x2)
{asm mov dx,0
asm mov ax,x2
asm sub ax,x3
asm mov ah,al
asm mov al,0
asm mov bx,y3
asm sub bx,y2
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope3,ax
asm mov aux,ax
asm mov byte ptr sign3,1 /*line3 has negative slope*/
asm mov byte ptr auxSign,1
}
else
{asm mov dx,0
asm mov ax,x3
asm sub ax,x2
asm mov ah,al
asm mov al,0
asm mov bx,y3
asm sub bx,y2
asm div bx /*ax:=256*(x2-x1) div (y2-y1)*/
asm mov slope3,ax
asm mov aux,ax
asm mov byte ptr sign3,0 /*line3 has positive slope*/
asm mov byte ptr auxSign,0
}
/*assign first and second*/
if (sign1)
{/*line1 is negative*/
if (sign2)
{/*both lines are negative*/
if (slope1>slope2)
{first=slope1; firstSign=sign1; last=slope2; lastSign=sign2; bend=2;}
else
{first=slope2; firstSign=sign2; last=slope1; lastSign=sign1; bend=1;}
}
else
{/*line1 is negative, but line2 is positive*/
first=slope1; firstSign=sign1; bend=2;
last=slope2; lastSign=sign2;
}
}
else
{/*line1 is positive*/
if (sign2)
{/*line1 is positive, but line2 is negative*/
first=slope2; firstSign=sign2; bend=1;
last=slope1; lastSign=sign1;
}
else
{/*both lines are positive*/
if (slope1>slope2)
{bend=1; first=slope2; firstSign=sign2; last=slope1; lastSign=sign1;}
else
{bend=2; first=slope1; firstSign=sign1; last=slope2; lastSign=sign2;}
}
}
bendCheck=y3-y2+1;
x2=320*y2+x2;
/*draw the triangle (finnaly)*/
/*; bx=screenpos of first line
; si=screenpos of last line
; dh=bx remainder
; dl=si remainder
; al=color*/
asm push es
asm push si
asm push di
asm mov ax,0a000h
asm mov es,ax /*;point es to screen*/
asm mov ax,y1
asm mov bx,320
asm mul bx
asm add ax,x1
asm mov bx,ax /*;bx=screen pos of first line*/
asm mov si,ax /*;si=screen pos of second line (they both start at point1)*/
asm mov cx,y3
asm sub cx,y1 /*;cx=height of triangle*/
asm mov al,c /*;color*/
asm mov ah,c
asm mov dx,0
asm cmp byte ptr firstSign,1
asm je l1out
asm cmp byte ptr lastSign,1
asm je l2out
asm jmp pospos
l2out:
asm jmp negpos
l1out:
asm cmp byte ptr lastSign,1
asm je l3out
asm jmp negpos
l3out:
asm jmp negneg
/*;shouldn't get here*/
/*; neg-pos*/
negpos:
asm push cx
asm mov cx,si
asm sub cx,bx
asm mov di,bx
asm test di,01
asm je l1np
asm stosb
asm dec cx
l1np:
asm cmp cx,0
asm jl l9np
asm shr cx,1
asm jc l2np
asm rep stosw
asm jmp l9np
l2np:
asm rep stosw
asm stosb /*;flags still preserved from shr cx,1*/
l9np:
asm add bx,320
asm sub dh,first
asm sbb bx,[first+1]
asm add si,320
asm add dl,last
asm adc si,[last+1]
asm pop cx
asm cmp cx,bendCheck
asm je l1npo
negPosEntry:
asm loop negpos /*;yloop*/
asm jmp outaHere
l1npo:
asm jmp switchdirs
/*; pos-neg*/
posneg:
asm push cx
asm mov cx,si
asm sub cx,bx
asm mov di,bx
asm test di,01
asm je l1pn
asm stosb
asm dec cx
l1pn:
asm cmp cx,0
asm jl l9pn
asm shr cx,1
asm jc l2pn
asm rep stosw
asm jmp l9pn
l2pn:
asm rep stosw
asm stosb /*;flags still preserved from shr cx,1*/
l9pn:
asm add bx,320
asm add dh,first
asm adc bx,[first+1]
asm add si,320
asm sub dl,last
asm sbb si,[last+1]
asm pop cx
asm cmp cx,bendCheck
asm je l1pno
posnegEntry:
asm loop posneg /*;yloop*/
asm jmp outaHere
l1pno:
asm jmp switchdirs
/*; neg-neg*/
negneg:
asm push cx
asm mov cx,si
asm sub cx,bx
asm mov di,bx
asm test di,01
asm je l1nn
asm stosb
asm dec cx
l1nn:
asm cmp cx,0
asm jl l9nn
asm shr cx,1
asm jc l2nn
asm rep stosw
asm jmp l9nn
l2nn:
asm rep stosw
asm stosb /*;flags still preserved from shr cx,1*/
l9nn:
asm add bx,320
asm sub dh,first
asm sbb bx,[first+1]
asm add si,320
asm sub dl,last
asm sbb si,[last+1]
asm pop cx
asm cmp cx,bendCheck
asm je l1nno
negnegEntry:
asm loop negneg /*;yloop*/
asm jmp outaHere
l1nno:
asm jmp switchdirs
/*; pos-pos*/
pospos:
asm push cx
asm mov cx,si
asm sub cx,bx
asm mov di,bx
asm test di,01
asm je l1pp
asm stosb
asm dec cx
l1pp:
asm cmp cx,0
asm jl l9pp
asm shr cx,1
asm jc l2pp
asm rep stosw
asm jmp l9pp
l2pp:
asm rep stosw
asm stosb /*;flags still preserved from shr cx,1*/
l9pp:
asm add bx,320
asm add dh,first
asm adc bx,[first+1]
asm add si,320
asm add dl,last
asm adc si,[last+1]
asm pop cx
asm cmp cx,bendCheck
asm je switchdirs
posposEntry:
asm loop pospos /*;yloop*/
asm jmp outaHere
switchdirs:
asm push ax
asm cmp byte ptr bend,2 /*;compare x pos of first with xpos of auxiliary*/
asm je l9sw
/*;first needs to bend*/
asm mov y2,si
asm mov bx,x2
asm mov ax,aux
asm mov first,ax
asm mov ah,auxSign
asm mov firstSign,ah
asm jmp l8sw
l9sw:
/*;last needs to bend*/
asm mov si,x2
asm mov ax,aux
asm mov last,ax
asm mov ah,auxSign
asm mov lastSign,ah
l8sw:
asm pop ax
asm cmp byte ptr firstSign,1
asm je l1sw
asm cmp byte ptr lastSign,1
asm je l2sw
asm jmp posposEntry
l2sw:
asm jmp posnegEntry
l1sw:
asm cmp byte ptr lastSign,1
asm je l3sw
asm jmp posnegEntry
l3sw:
asm jmp negnegEntry
/*;shouldn't get here*/
outaHere:
asm pop di
asm pop si
asm pop es
}